package com.sywb.chuangyebao.view;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.luck.picture.lib.tools.PictureFileUtils;
import com.sywb.chuangyebao.Constants;
import com.sywb.chuangyebao.R;
import com.sywb.chuangyebao.bean.DistrictInfo;
import com.sywb.chuangyebao.bean.FileConfigInfo;
import com.sywb.chuangyebao.bean.InterestIndustryInfo;
import com.sywb.chuangyebao.bean.UploadInfo;
import com.sywb.chuangyebao.bean.UserData;
import com.sywb.chuangyebao.bean.UserInfo;
import com.sywb.chuangyebao.contract.Contract;
import com.sywb.chuangyebao.utils.GlideUtils;
import com.sywb.chuangyebao.utils.HttpCallback;
import com.sywb.chuangyebao.utils.HttpUtils;
import com.sywb.chuangyebao.view.dialog.SexSelectDialog;
import com.sywb.chuangyebao.view.dialog.WheelViewDialog;

import org.bining.footstone.db.DbManager;
import org.bining.footstone.log.Logger;
import org.bining.footstone.rxjava.luban.Luban;
import org.bining.footstone.rxjava.luban.OnCompressListener;
import org.bining.footstone.rxjava.rxbus.RxBus;
import org.bining.footstone.rxjava.rxbus.annotation.Subscribe;
import org.bining.footstone.rxjava.rxbus.annotation.Tag;
import org.bining.footstone.rxjava.rxbus.thread.ThreadMode;
import org.bining.footstone.utils.FileUtils;
import org.bining.footstone.utils.SharedUtils;
import org.bining.footstone.utils.ToastUtils;

import java.io.File;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class UserDataActivity extends ActionbarActivity<Contract.Presenter> implements Contract.View {

    @BindView(R.id.iv_avatar)
    ImageView ivAvatar;
    @BindView(R.id.rl_avatar)
    RelativeLayout rlAvatar;
    @BindView(R.id.iv_index)
    ImageView ivIndex;
    @BindView(R.id.rl_index)
    RelativeLayout rlIndex;
    @BindView(R.id.tv_nickname)
    TextView tvNickname;
    @BindView(R.id.rl_nickname)
    RelativeLayout rlNickname;
    @BindView(R.id.tv_sex)
    TextView tvSex;
    @BindView(R.id.rl_sex)
    RelativeLayout rlSex;
    @BindView(R.id.tv_age)
    TextView tvAge;
    @BindView(R.id.rl_age)
    RelativeLayout rlAge;
    @BindView(R.id.tv_area)
    TextView tvArea;
    @BindView(R.id.rl_area)
    RelativeLayout rlArea;
    @BindView(R.id.tv_good_area)
    TextView tvGoodArea;
    @BindView(R.id.rl_good_area)
    RelativeLayout rlGoodArea;
    @BindView(R.id.tv_entre_stage)
    TextView tvEntreStage;
    @BindView(R.id.tv_investment_budget)
    TextView tvInvestmentBudget;
    @BindView(R.id.rl_entre_stage)
    RelativeLayout rlEntreStage;
    @BindView(R.id.tv_sign)
    TextView tvSign;
    @BindView(R.id.rl_sign)
    RelativeLayout rlSign;
    @BindView(R.id.tv_phone_no)
    TextView tvPhoneNo;

    @BindView(R.id.rl_investment_budget)
    RelativeLayout rlInvestmentBudget;
    @BindView(R.id.line_investment_budget)
    View lineInvestmentBudget;

    private String openID;
    private UserInfo userInfo;
    private UserData userData;

    @Override
    public int getLayoutId(Bundle bundle) {
        return R.layout.activity_user_data;
    }

    @Override
    public void initView(Bundle bundle) {
        super.initView(bundle);
        setTitle(R.string.personal_data);
        initData();
    }

    @Override
    public void initPresenter() {
        mPresenter.initPresenter(this);
    }

    public void initData() {
        openID = SharedUtils.getString(Constants.USEROPENID, "");
        if (!TextUtils.isEmpty(openID)) {

            userInfo = DbManager.getInstance().queryById(openID, UserInfo.class);
            if (userInfo != null) {

                GlideUtils.getCircleImageToUrl(mActivity, userInfo.getShowAvatar(), ivAvatar);

                tvNickname.setText(userInfo.getShowName());

                if (!TextUtils.isEmpty(userInfo.brief_intro)) {
                    tvSign.setText(userInfo.brief_intro);
                } else {
                    tvSign.setText(R.string.input_sign);
                }

                int defBgImg = 0;
                int userRole = userInfo.getUserRole();
                switch (userRole) {
                    case Constants.USER_TYPE_PROJECT:
                        defBgImg = R.drawable.project_number_bg;
                        break;
                    case Constants.USER_TYPE_SERVICE:
                        defBgImg = R.drawable.provider_bg;
                        break;
                    case Constants.USER_TYPE_TUTOR:
                        defBgImg = R.drawable.tutor_bg;
                        break;
                    default:
                        defBgImg = R.drawable.user_bg;
                        rlInvestmentBudget.setVisibility(View.VISIBLE);
                        lineInvestmentBudget.setVisibility(View.VISIBLE);
                        break;
                }

                if (!TextUtils.isEmpty(userInfo.bg_img)) {
                    GlideUtils.getNormalIamgeToUrl(mActivity, userInfo.bg_img, ivIndex, defBgImg);
                } else {
                    ivIndex.setImageResource(defBgImg);
                }

                if (TextUtils.isEmpty(userInfo.mobile)) {
                    tvPhoneNo.setTextColor(ContextCompat.getColor(mContext, R.color.colorYellow));
                    tvPhoneNo.setText(R.string.bind_phone_no);
                } else {
                    tvPhoneNo.setTextColor(ContextCompat.getColor(mContext, R.color.colorGray));
                    tvPhoneNo.setText(userInfo.mobile);
                }
            }

            userData = DbManager.getInstance().queryById(openID, UserData.class);
            if (userData != null) {

                String sex = null;
                switch (userData.gender) {
                    case 0:
                        sex = "未设置";
                        break;
                    case 1:
                        sex = "男";
                        break;
                    case 2:
                        sex = "女";
                        break;
                }
                tvSex.setText(sex);

                Date date = new Date();
                date.getYear();

                if (userData.birth_year == 0 || userData.birth_month == 0 || userData.birth_day == 0) {
                    tvAge.setText(R.string.input_age);
                } else {
                    Calendar calendar = Calendar.getInstance();
                    int year = calendar.get(Calendar.YEAR);
                    int month = calendar.get(Calendar.MONTH) + 1;
                    int day = calendar.get(Calendar.DAY_OF_MONTH);

                    int age = year - userData.birth_year;

                    if (month < userData.birth_month) {
                        age--;
                    } else if (month == userData.birth_month) {
                        if (day < userData.birth_day) {
                            age--;
                        }
                    }
                    tvAge.setText(String.valueOf(age));
                }

                if (!TextUtils.isEmpty(userData.area)) {
                    DistrictInfo areaInfo = DbManager.getInstance().queryById(userData.area, DistrictInfo.class);
                    if (areaInfo != null) {
                        StringBuilder area = new StringBuilder();
                        if (!areaInfo.parentId.equals("0")) {
                            DistrictInfo provinceInfo = DbManager.getInstance().queryById(areaInfo.parentId, DistrictInfo.class);
                            if (provinceInfo != null) {
                                area.append(provinceInfo.name).append(Constants.SPACE);
                            }
                        }
                        area.append(areaInfo.name);

                        tvArea.setText(area.toString());
                    } else {
                        tvArea.setText(R.string.input_area);
                    }
                } else {
                    tvArea.setText(R.string.input_area);
                }

                int investmentBudget = userData.investment_budget;
                if (investmentBudget > 0) {
                    tvInvestmentBudget.setText(getResources().getStringArray(R.array.investment_budget)[investmentBudget - 1]);
                } else {
                    tvInvestmentBudget.setText(R.string.input_investment_budget);
                }

            }

            List<InterestIndustryInfo> list = DbManager.getInstance().query(InterestIndustryInfo.class);
            if (list == null || list.size() == 0) {
                tvGoodArea.setText(R.string.input_good_area);
            } else {
                StringBuffer goodArae = new StringBuffer();
                for (int i = 0; i < list.size(); i++) {
                    InterestIndustryInfo goodAreaInfo = list.get(i);
                    if (goodAreaInfo.isChecked) {
                        goodArae.append(goodAreaInfo.name).append(Constants.COMMA);
                    }
                }
                int length = goodArae.length();
                if (length > 0) {
                    goodArae.delete(length - 1, length);
                    tvGoodArea.setText(goodArae.toString());
                } else {
                    tvGoodArea.setText(R.string.input_good_area);
                }
            }

            int period = SharedUtils.getInt(Constants.USERBUSINESSPERIOD, 0);
            if (period > 0) {
                String[] periods = getResources().getStringArray(R.array.period_types);
                tvEntreStage.setText(periods[period - 1]);
            } else {
                tvEntreStage.setText(R.string.input_entre_stage);
            }

        } else {
            RxBus.get().post(Constants.HTTP_SSO_LOGIN, Constants.HTTP_SSO_LOGIN);
            exit();
        }
    }

    @Subscribe(thread = ThreadMode.MAIN_THREAD, tags = {@Tag(Constants.TABLE_USERINFO), @Tag(Constants.TABLE_USERDATA), @Tag(Constants.TABLE_INTERESTINDUSTRY)})
    public void rxUserInfoChnage(String tableName) {
        initData();
    }

    @OnClick({R.id.rl_avatar, R.id.rl_index, R.id.rl_nickname, R.id.rl_sex, R.id.rl_age, R.id.rl_area, R.id.rl_good_area, R.id.rl_entre_stage, R.id.rl_sign, R.id.rl_investment_budget, R.id.rl_phone_no})
    public void onClick(View view) {
        if (!isCanClick(view)) return;
        switch (view.getId()) {
            case R.id.rl_avatar:
//                AvatarSelectDialog avatarDialog = AvatarSelectDialog.newInstance("设置头像");
//                avatarDialog.show(getFragmentManager(), "");

                // new ImgSelConfig.Builder().statusBarLightMode(true).backResId(R.drawable.ic_back_white).title("选择头像").maxNum(1).needCamera(true).needCrop(true).build(mActivity, 10001);

                // 进入相册,用不到的api可以不写
                PictureSelector.create(mActivity)
                        .openGallery(PictureMimeType.ofImage())//全部.PictureMimeType.ofAll()、图片.ofImage()、视频.ofVideo()、音频.ofAudio()
                        .theme(R.style.style_picture_white)//主题样式(不设置为默认样式) 也可参考demo values/styles下 例如：R.style.picture.white.style
                        .maxSelectNum(1)// 最大图片选择数量 int
                        .minSelectNum(0)// 最小选择数量 int
                        .imageSpanCount(4)// 每行显示个数 int
                        .selectionMode(PictureConfig.SINGLE)// 多选 or 单选 PictureConfig.MULTIPLE or PictureConfig.SINGLE
                        .previewImage(true)// 是否可预览图片 true or false
                        .previewVideo(false)// 是否可预览视频 true or false
                        .enablePreviewAudio(false) // 是否可播放音频 true or false
                        .isCamera(true)// 是否显示拍照按钮 true or false
                        .imageFormat(PictureMimeType.PNG)// 拍照保存图片格式后缀,默认jpeg
                        .isZoomAnim(true)// 图片列表点击 缩放效果 默认true
                        .sizeMultiplier(0.5f)// glide 加载图片大小 0~1之间 如设置 .glideOverride()无效
                        .setOutputCameraPath(FileUtils.getAppDefPath(FileUtils.PATH_IMAGE))// 自定义拍照保存路径,可不填
                        .enableCrop(true)// 是否裁剪 true or false
                        .compress(true)// 是否压缩 true or false
                        // .glideOverride()// int glide 加载宽高，越小图片列表越流畅，但会影响列表图片浏览的清晰度
                        .withAspectRatio(1, 1)// int 裁剪比例 如16:9 3:2 3:4 1:1 可自定义
                        // .hideBottomControls()// 是否显示uCrop工具栏，默认不显示 true or false
                        // .isGif()// 是否显示gif图片 true or false
                        .compressSavePath(FileUtils.getAppDefPath(FileUtils.PATH_IMAGE))//压缩图片保存地址
                        .freeStyleCropEnabled(false)// 裁剪框是否可拖拽 true or false
                        .circleDimmedLayer(false)// 是否圆形裁剪 true or false
                        .showCropFrame(true)// 是否显示裁剪矩形边框 圆形裁剪时建议设为false   true or false
                        .showCropGrid(true)// 是否显示裁剪矩形网格 圆形裁剪时建议设为false    true or false
                        .openClickSound(false)// 是否开启点击声音 true or false
                        // .selectionMedia()// 是否传入已选图片 List<LocalMedia> list
                        // .previewEggs()// 预览图片时 是否增强左右滑动图片体验(图片滑动一半即可看到上一张是否选中) true or false
                        // .cropCompressQuality()// 裁剪压缩质量 默认90 int
                        .minimumCompressSize(100)// 小于100kb的图片不压缩
                        .synOrAsy(true)//同步true或异步false 压缩 默认同步
                        // .cropWH()// 裁剪宽高比，设置如果大于图片本身宽高则无效 int
                        .rotateEnabled(true) // 裁剪是否可旋转图片 true or false
                        .scaleEnabled(true)// 裁剪是否可放大缩小图片 true or false
                        // .videoQuality()// 视频录制质量 0 or 1 int
                        //  .videoMaxSecond(15)// 显示多少秒以内的视频or音频也可适用 int
                        // .videoMinSecond(10)// 显示多少秒以内的视频or音频也可适用 int
                        // .recordVideoSecond()//视频秒数录制 默认60s int
                        .forResult(10001);//结果回调onActivityResult code
                break;
            case R.id.rl_index:
//                AvatarSelectDialog indexDialog = AvatarSelectDialog.newInstance("更换封面");
//                indexDialog.show(getFragmentManager(), "");

                // new ImgSelConfig.Builder().statusBarLightMode(true).backResId(R.drawable.ic_back_white).title("选择背景图").maxNum(1).needCamera(true).needCrop(true).cropSize(1125, 495, 1125, 495).build(mActivity, 10002);

                // 进入相册,用不到的api可以不写
                PictureSelector.create(mActivity)
                        .openGallery(PictureMimeType.ofImage())//全部.PictureMimeType.ofAll()、图片.ofImage()、视频.ofVideo()、音频.ofAudio()
                        .theme(R.style.style_picture_white)//主题样式(不设置为默认样式) 也可参考demo values/styles下 例如：R.style.picture.white.style
                        .maxSelectNum(1)// 最大图片选择数量 int
                        .minSelectNum(0)// 最小选择数量 int
                        .imageSpanCount(4)// 每行显示个数 int
                        .selectionMode(PictureConfig.SINGLE)// 多选 or 单选 PictureConfig.MULTIPLE or PictureConfig.SINGLE
                        .previewImage(true)// 是否可预览图片 true or false
                        .previewVideo(false)// 是否可预览视频 true or false
                        .enablePreviewAudio(false) // 是否可播放音频 true or false
                        .isCamera(true)// 是否显示拍照按钮 true or false
                        .imageFormat(PictureMimeType.PNG)// 拍照保存图片格式后缀,默认jpeg
                        .isZoomAnim(true)// 图片列表点击 缩放效果 默认true
                        .sizeMultiplier(0.5f)// glide 加载图片大小 0~1之间 如设置 .glideOverride()无效
                        .setOutputCameraPath(FileUtils.getAppDefPath(FileUtils.PATH_IMAGE))// 自定义拍照保存路径,可不填
                        .enableCrop(true)// 是否裁剪 true or false
                        .compress(true)// 是否压缩 true or false
                        // .glideOverride()// int glide 加载宽高，越小图片列表越流畅，但会影响列表图片浏览的清晰度
                        .withAspectRatio(1125, 495)// int 裁剪比例 如16:9 3:2 3:4 1:1 可自定义
                        // .hideBottomControls()// 是否显示uCrop工具栏，默认不显示 true or false
                        // .isGif()// 是否显示gif图片 true or false
                        .compressSavePath(FileUtils.getAppDefPath(FileUtils.PATH_IMAGE))//压缩图片保存地址
                        .freeStyleCropEnabled(false)// 裁剪框是否可拖拽 true or false
                        .circleDimmedLayer(false)// 是否圆形裁剪 true or false
                        .showCropFrame(true)// 是否显示裁剪矩形边框 圆形裁剪时建议设为false   true or false
                        .showCropGrid(true)// 是否显示裁剪矩形网格 圆形裁剪时建议设为false    true or false
                        .openClickSound(false)// 是否开启点击声音 true or false
                        // .selectionMedia()// 是否传入已选图片 List<LocalMedia> list
                        // .previewEggs()// 预览图片时 是否增强左右滑动图片体验(图片滑动一半即可看到上一张是否选中) true or false
                        // .cropCompressQuality()// 裁剪压缩质量 默认90 int
                        .minimumCompressSize(100)// 小于100kb的图片不压缩
                        .synOrAsy(true)//同步true或异步false 压缩 默认同步
                        // .cropWH()// 裁剪宽高比，设置如果大于图片本身宽高则无效 int
                        .rotateEnabled(true) // 裁剪是否可旋转图片 true or false
                        .scaleEnabled(true)// 裁剪是否可放大缩小图片 true or false
                        // .videoQuality()// 视频录制质量 0 or 1 int
                        //  .videoMaxSecond(15)// 显示多少秒以内的视频or音频也可适用 int
                        // .videoMinSecond(10)// 显示多少秒以内的视频or音频也可适用 int
                        // .recordVideoSecond()//视频秒数录制 默认60s int
                        .forResult(10002);//结果回调onActivityResult code


                break;
            case R.id.rl_nickname:
                advance(UserNameActivity.class, openID, tvNickname.getText().toString());
                break;
            case R.id.rl_sex:
                if (userData == null) {
                    return;
                }

                SexSelectDialog sexSelectDialog = SexSelectDialog.newInstance(getString(R.string.text_sex), userData.gender);
                sexSelectDialog.setOnItemListener(new SexSelectDialog.OnItemListener() {
                    @Override
                    public void onResult(int postion, boolean isSuccess, String message) {
                        userData.gender = postion;
                        HttpUtils.updateUserData(new String[]{"gender"}, new Object[]{userData.gender}, new HttpCallback<String>() {

                            @Override
                            public void onStart() {
                                super.onStart();
                                showProgress();
                            }

                            @Override
                            public void onSuccess(String s) {
                                DbManager.getInstance().update(userData);
                            }

                            @Override
                            public void onError(String strMsg) {
                                super.onError(strMsg);
                                ToastUtils.show(mContext, strMsg);
                            }

                            @Override
                            public void onFinish() {
                                super.onFinish();
                                hideProgress();
                            }
                        });
                    }
                });
                sexSelectDialog.show(getMyFragmentManager(), "Sex");

//                WheelViewDialog sexDialog = WheelViewDialog.newInstance(1, userData.gender);
//                sexDialog.setSelectedCompleteListener(new WheelViewDialog.SelectedCompleteListener() {
//                    @Override
//                    public void completeSelect(int[] i, String[] parmas) {
//                        userData.gender = i[0] + 1;
//                        HttpUtils.updateUserData(new String[]{"gender"}, new Object[]{userData.gender}, new HttpCallback<String>() {
//
//                            @Override
//                            public void onStart() {
//                                super.onStart();
//                                showProgress();
//                            }
//
//                            @Override
//                            public void onSuccess(String s) {
//                                DbManager.getInstance().update(userData);
//                            }
//
//                            @Override
//                            public void onError(String strMsg) {
//                                super.onError(strMsg);
//                                ToastUtils.show(mContext, strMsg);
//                            }
//
//                            @Override
//                            public void onFinish() {
//                                super.onFinish();
//                                hideProgress();
//                            }
//                        });
//                    }
//                });
//                sexDialog.show(mActivity.getFragmentManager(), "Sex");
                // sexDialog.initSexData(userData.gender);
                break;
            case R.id.rl_age:
                if (userData == null) {
                    return;
                }
                WheelViewDialog ageDialog = WheelViewDialog.newInstance(2, userData.birth_year, userData.birth_month, userData.birth_day);
                ageDialog.setSelectedCompleteListener(new WheelViewDialog.SelectedCompleteListener() {
                    @Override
                    public void completeSelect(int[] i, String[] params) {
                        userData.birth_year = Integer.valueOf(params[0]);
                        userData.birth_month = Integer.valueOf(params[1]);
                        userData.birth_day = Integer.valueOf(params[2]);

                        HttpUtils.updateUserData(new String[]{"birth_year", "birth_month", "birth_day"}, new Object[]{userData.birth_year, userData.birth_month, userData.birth_day}, new HttpCallback<String>() {

                            @Override
                            public void onStart() {
                                super.onStart();
                                showProgress();
                            }

                            @Override
                            public void onSuccess(String s) {
                                DbManager.getInstance().update(userData);
                            }

                            @Override
                            public void onError(String strMsg) {
                                super.onError(strMsg);
                                ToastUtils.show(mContext, strMsg);
                            }

                            @Override
                            public void onFinish() {
                                super.onFinish();
                                hideProgress();
                            }
                        });
                    }
                });
                ageDialog.show(getMyFragmentManager(), "Age");
                // ageDialog.initAgeData(userData.birth_year, userData.birth_month, userData.birth_day);
                break;
            case R.id.rl_area:
                if (userData == null) {
                    return;
                }
                WheelViewDialog areaDialog = WheelViewDialog.newInstance(3, userData.area);
                areaDialog.setSelectedCompleteListener(new WheelViewDialog.SelectedCompleteListener() {
                    @Override
                    public void completeSelect(int[] i, String[] params) {
                        userData.area = params[0];
                        HttpUtils.updateUserData(new String[]{"area"}, new Object[]{userData.area}, new HttpCallback<String>() {

                            @Override
                            public void onStart() {
                                super.onStart();
                                showProgress();
                            }

                            @Override
                            public void onSuccess(String s) {
                                DbManager.getInstance().update(userData);
                            }

                            @Override
                            public void onError(String strMsg) {
                                super.onError(strMsg);
                                ToastUtils.show(mContext, strMsg);
                            }

                            @Override
                            public void onFinish() {
                                super.onFinish();
                                hideProgress();
                            }
                        });
                    }
                });
                areaDialog.show(getMyFragmentManager(), "Area");
                // areaDialog.initAreaData(userData.area);
                break;
            case R.id.rl_good_area:
                advance(ListCheckActivity.class, getString(R.string.good_area));
                break;
            case R.id.rl_entre_stage:
                int period = SharedUtils.getInt(Constants.USERBUSINESSPERIOD, 0);
                WheelViewDialog periodDialog = WheelViewDialog.newInstance(4, period - 1);
                periodDialog.setSelectedCompleteListener(new WheelViewDialog.SelectedCompleteListener() {
                    @Override
                    public void completeSelect(int[] i, String[] parmas) {
                        int chuangye_stage = i[0] + 1;
                        HttpUtils.goodatUpdate(chuangye_stage, null, 0, new HttpCallback<Object>(chuangye_stage) {
                            @Override
                            public void onSuccess(Object o) {
                                int chuangye_stage = (int) data;

                                SharedUtils.put(Constants.USERBUSINESSPERIOD, chuangye_stage);
                                String[] periods = getResources().getStringArray(R.array.period_types);
                                tvEntreStage.setText(periods[chuangye_stage - 1]);

                                if (userData != null) {
                                    userData.chuangye_stage = chuangye_stage;
                                    DbManager.getInstance().update(userData);
                                }
                            }

                            @Override
                            public void onError(String strMsg) {
                                super.onError(strMsg);
                                ToastUtils.show(mContext, strMsg);
                            }
                        });
                    }
                });
                periodDialog.show(getMyFragmentManager(), "EntreStage");
                // periodDialog.initPeriodData(period - 1);
                break;
            case R.id.rl_investment_budget: // 投资预算
                if (userData == null) {
                    return;
                }
                int oldInvestmentBudget = userData.investment_budget;
                WheelViewDialog budgetDialog = WheelViewDialog.newInstance(6, oldInvestmentBudget - 1);
                budgetDialog.setSelectedCompleteListener(new WheelViewDialog.SelectedCompleteListener() {
                    @Override
                    public void completeSelect(final int[] i, final String[] parmas) {
                        tvInvestmentBudget.setText(parmas[0]);
                        int newInvestmentBudget = i[0] + 1;
                        HttpUtils.goodatUpdate(0, null, i[0] + 1, new HttpCallback<Object>(newInvestmentBudget) {
                            @Override
                            public void onSuccess(Object o) {
                                userData.investment_budget = (int) data;
                                DbManager.getInstance().update(userData);
                            }

                            @Override
                            public void onError(String strMsg) {
                                super.onError(strMsg);
                                ToastUtils.show(mContext, strMsg);
                            }
                        });
                    }
                });
                budgetDialog.show(getMyFragmentManager(), "budget");
                break;
            case R.id.rl_phone_no: // 手机号码
                if (TextUtils.isEmpty(userInfo.mobile)) {
                    advance(BindMobileActivity.class);
                } else {
                    advance(BindMobileSuccessActivity.class, userInfo.mobile);
                }
                break;
            case R.id.rl_sign:
                advance(UserOpinionActivity.class, getString(R.string.personal_sign), userInfo != null ? userInfo.brief_intro : "");
                break;
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {

//                case PictureConfig.CHOOSE_REQUEST:
//                    // 图片选择结果回调
//                    List<LocalMedia> selectList = PictureSelector.obtainMultipleResult(data);
//                    if (selectList != null && selectList.size() > 0) {
//                        llPic.setVisibility(View.VISIBLE);
//
//                        List<String> imageList = new ArrayList<>();
//                        for (LocalMedia localMedia : selectList) {
//                            imageList.add(media.isCompressed() ? media.getCompressPath() : media.getPath());
//                        }
//                        mPresenter.addPic(imageList);
//                    }
//                    // 例如 LocalMedia 里面返回三种path
//                    // 1.media.getPath(); 为原图path
//                    // 2.media.getCutPath();为裁剪后path，需判断media.isCut();是否为true
//                    // 3.media.getCompressPath();为压缩后path，需判断media.isCompressed();是否为true
//                    // 如果裁剪并压缩了，以取压缩路径为准，因为是先裁剪后压缩的
//                    break;

                case 10001:
                    // List<String> faceList = JSON.parseArray(data.getStringExtra("p0"), String.class);
                    List<LocalMedia> selectList = PictureSelector.obtainMultipleResult(data);
                    if (selectList == null || selectList.size() == 0) {
                        return;
                    }
                    String path = selectList.get(0).getCompressPath();
                    Logger.e("Origla :" + path);
                    Luban.with(mContext).load(path).setCompressListener(new OnCompressListener() {
                        @Override
                        public void onStart() {
                            showProgress();
                        }

                        @Override
                        public void onSuccess(List<File> list) {
                            File file = list.get(0);
                            Logger.e("Luban :" + file.getAbsolutePath());
                            String fileConfig = SharedUtils.getString(Constants.USERFILECONFIG, "");
                            if (!TextUtils.isEmpty(fileConfig)) {
                                FileConfigInfo fileConfigInfo = JSON.parseObject(fileConfig, FileConfigInfo.class);
                                if (fileConfigInfo != null) {
                                    try {
                                        String fileType = FileUtils.getFileType(file.getAbsolutePath());
                                        int fileMaxSize = fileConfigInfo.mime_types.get(fileType).size;
                                        if (FileUtils.getFileSize(file) <= fileMaxSize) {
                                            HttpUtils.uploadHttp(file, new HttpCallback<List<UploadInfo>>() {

                                                @Override
                                                public void onStart() {
                                                    super.onStart();
                                                    showProgress();
                                                }

                                                @Override
                                                public void onSuccess(List<UploadInfo> uploadInfos) {
                                                    HttpUtils.updateUserData(new String[]{"profile_photo"}, new Object[]{uploadInfos.get(0).filename}, new HttpCallback<String>() {

                                                        @Override
                                                        public void onStart() {
                                                            super.onStart();
                                                            showProgress();
                                                        }

                                                        @Override
                                                        public void onSuccess(String s) {
                                                            String avatar = JSON.parseObject(s).getString("profile_photo");
                                                            userInfo.avatar_hd = avatar;
                                                            userInfo.avatar_l = avatar;
                                                            userInfo.avatar_s = avatar;
                                                            DbManager.getInstance().update(userInfo);

                                                            PictureFileUtils.deleteCacheDirFile(mActivity);
                                                        }

                                                        @Override
                                                        public void onError(String strMsg) {
                                                            super.onError(strMsg);
                                                            ToastUtils.show(mContext, strMsg);
                                                        }

                                                        @Override
                                                        public void onFinish() {
                                                            super.onFinish();
                                                            hideProgress();
                                                        }
                                                    });
                                                }

                                                @Override
                                                public void onError(String strMsg) {
                                                    super.onError(strMsg);
                                                    ToastUtils.show(mContext, strMsg);
                                                    hideProgress();
                                                }

                                                @Override
                                                public void onFinish() {
                                                    super.onFinish();
                                                    hideProgress();
                                                }
                                            });
                                            return;
                                        } else {
                                            ToastUtils.show(mContext, "您选择的图片太大了");
                                            hideProgress();
                                            return;
                                        }
                                    } catch (Exception ingon) {
                                    }
                                }
                            }
                            ToastUtils.show(mContext, "更新头像失败，请重试！");
                            hideProgress();
                        }

                        @Override
                        public void onError(Throwable throwable) {
                            hideProgress();
                            Logger.e(throwable.getMessage());
                            ToastUtils.show(mContext, "更新头像失败，请重试！");
                        }
                    }).launch();
                    break;
                case 10002:
                    List<LocalMedia> imageList = PictureSelector.obtainMultipleResult(data);
                    if (imageList == null || imageList.size() == 0) {
                        return;
                    }
                    // List<String> imageList = JSON.parseArray(data.getStringExtra("p0"), String.class);
                    String indexPath = imageList.get(0).getCompressPath();
                    Luban.with(mContext).load(indexPath).setCompressListener(new OnCompressListener() {
                        @Override
                        public void onStart() {
                            showProgress();
                        }

                        @Override
                        public void onSuccess(List<File> list) {
                            File file = list.get(0);
                            String fileConfig = SharedUtils.getString(Constants.USERFILECONFIG, "");
                            if (!TextUtils.isEmpty(fileConfig)) {
                                FileConfigInfo fileConfigInfo = JSON.parseObject(fileConfig, FileConfigInfo.class);
                                if (fileConfigInfo != null) {
                                    try {
                                        String fileType = FileUtils.getFileType(file.getAbsolutePath());
                                        int fileMaxSize = fileConfigInfo.mime_types.get(fileType).size;
                                        if (FileUtils.getFileSize(file) <= fileMaxSize) {
                                            HttpUtils.uploadHttp(file, new HttpCallback<List<UploadInfo>>() {

                                                @Override
                                                public void onSuccess(List<UploadInfo> uploadInfos) {
                                                    HttpUtils.updateUserData(new String[]{"bg_img"}, new Object[]{uploadInfos.get(0).filename}, new HttpCallback<String>() {

                                                        @Override
                                                        public void onStart() {
                                                            super.onStart();
                                                            showProgress();
                                                        }

                                                        @Override
                                                        public void onSuccess(String s) {
                                                            userInfo.bg_img = JSON.parseObject(s).getString("bg_img");
                                                            DbManager.getInstance().update(userInfo);

                                                            PictureFileUtils.deleteCacheDirFile(mActivity);
                                                        }

                                                        @Override
                                                        public void onError(String strMsg) {
                                                            super.onError(strMsg);
                                                            ToastUtils.show(mContext, strMsg);
                                                        }

                                                        @Override
                                                        public void onFinish() {
                                                            super.onFinish();
                                                            hideProgress();
                                                        }
                                                    });
                                                }

                                                @Override
                                                public void onError(String strMsg) {
                                                    super.onError(strMsg);
                                                    ToastUtils.show(mContext, strMsg);
                                                    hideProgress();
                                                }

                                                @Override
                                                public void onFinish() {
                                                    super.onFinish();
                                                    hideProgress();
                                                }
                                            });
                                            return;
                                        } else {
                                            ToastUtils.show(mContext, "您选择的图片太大了");
                                            hideProgress();
                                            return;
                                        }
                                    } catch (Exception ingon) {
                                    }
                                }
                            }
                            ToastUtils.show(mContext, "更新主页背景失败，请重试！");
                            hideProgress();
                        }

                        @Override
                        public void onError(Throwable throwable) {
                            hideProgress();
                            ToastUtils.show(mContext, "更新主页背景失败，请重试！");
                        }
                    }).launch();
                    break;

            }
        }
    }

    @Override
    public boolean useRxBus() {
        return true;
    }
}